package leetcode.datastructures.queue;

import lombok.Data;

import java.util.Scanner;

/**
 * @author shilei
 * @create 2021-10-08 15:21
 * 演示数组模拟队列
 */
public class ArrayQueueDemo {

    public static void main(String[] args) {
        //创建队列
        ArrayQueue arrayQueue = new ArrayQueue(3);
        char key = ' ';//接收用户输入指令
        Scanner scanner = new Scanner(System.in);
        boolean loop = true;
        while (loop) {
            System.out.println("s(show): 显示队列");
            System.out.println("e(exit): 退出程序");
            System.out.println("a(add):  添加队列");
            System.out.println("g(get):  获取队列");
            System.out.println("h(head): 查看队列头数据");
            key = scanner.next().charAt(0);
            switch (key) {
                case 's':
                    arrayQueue.showQueue();
                    break;
                case 'a':
                    System.out.println("输出一个数");
                    arrayQueue.addQueue(scanner.nextInt());
                    break;
                case 'g':
                    try {
                        int queue = arrayQueue.getQueue();
                        System.out.printf("取出来的数据为:%d\n", queue);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'h':
                    try {
                        int headQueue = arrayQueue.headQueue();
                        System.out.printf("头数据为:%d\n", headQueue);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'e':
                    scanner.close();
                    loop = false;
                    break;
                default:
                    break;
            }
        }
        System.out.println("程序退出~~");
    }


}

//使用数组模拟队列
@Data
class ArrayQueue {
    private int maxSize; //表示数组最大容量
    private int front; //队列头
    private int rear;//队列尾
    private int[] arr; //存放数据 模拟队列

    public ArrayQueue(int maxSize) {
        this.maxSize = maxSize;
        arr = new int[maxSize];
        front = -1; //指向队列头部 分析出 front 是指向队列头部的前一个位置
        rear = -1;//指向队列尾部 指向队列尾的数据 (队列最后一个数据)
    }

    //判断是否队列满了
    public boolean isFull() {
        return rear == maxSize - 1;
    }

    //判断队列是否为空
    public boolean isEmpty() {
        return rear == front;
    }

    //添加数据到队列
    public void addQueue(int data) {
        if (isFull()) {
            System.out.println("队列满了~~~");
            return;
        }
        rear++; //让rear 后移
        arr[rear] = data;
    }

    //获取队列数据
    public int getQueue() {
        //判断是否为空
        if (isEmpty()) {
            System.out.println("队列为空~~~");
            throw new RuntimeException("队列为空");
        }
        front++; //front 后移
        return arr[front];

    }

    //显示队列所有数据
    public void showQueue() {
        if (isEmpty()) {
            System.out.println("队列为空");
            return;
        }
        //遍历数据
        for (int i = 0; i < arr.length; i++) {
            System.out.printf("arr[%d]=%d\n", i, arr[i]);
        }
    }

    //显示队列头数据 不是取数据
    public int headQueue() {
        if (isEmpty()) {
            throw new RuntimeException("队列为空");
        }
        return arr[front + 1];
    }
}